package com.hq.fxh.No5_User.Jianli.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hq.fxh.CustomerView.PopupWindow.PopupForAddress;
import com.hq.fxh.CustomerView.PopupWindow.PopupForChangeInfo;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.JianLiEduModle;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.TimeSelectUtils;
import com.hq.fxh.Utils.Tool;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/21
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_JianLi_AddEducation extends Fragment implements View.OnClickListener {
    protected View rootView;
    protected TextView tvJianliEducationName;
    protected TextView tvJianliEducationZhuanYe;
    protected TextView tvJianliEducationTimeOut;
    protected TextView tvJianliWorkXue;
    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected LinearLayout toolbarContent;
    protected TextView btnDeleteEdu;
    private PopupForChangeInfo popup;
    int type = 0;
    private PopupForAddress popupXueli;
    private TimeSelectUtils timeSelectUtils;
    private JianLiEduModle jianLiEduModle;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_jianliset_education, null);
        initView(rootView);
        initData();

        return rootView;
    }


    public void setData(String data) {
        if (data != null) {
            jianLiEduModle = new Gson().fromJson(data, JianLiEduModle.class);
        } else {
            jianLiEduModle = new JianLiEduModle();
        }


    }

    private void setViewData() {
        Tool.setStringToText(tvJianliEducationName, jianLiEduModle.getSchool());
        Tool.setStringToText(tvJianliEducationZhuanYe, jianLiEduModle.getProfession());
        String graduate_dt = jianLiEduModle.getGraduate_dt();
        String substring = graduate_dt.substring(0, graduate_dt.indexOf(" "));
        Tool.setStringToText(tvJianliEducationTimeOut, substring);
    }

    private void initData() {
        /// 最高学历 0未知(默认值) 1 中专及以下 2高中 3大专 4本科 5硕士 6博士

        setViewData();
        int edu = jianLiEduModle.getEdu();
        switch (edu) {
            case 0:

                break;
            case 1:
                Tool.setStringToText(tvJianliWorkXue, "中专及以下");

                break;
            case 2:
                Tool.setStringToText(tvJianliWorkXue, "高中");

                break;
            case 3:
                Tool.setStringToText(tvJianliWorkXue, "大专");

                break;
            case 4:
                Tool.setStringToText(tvJianliWorkXue, "本科");

                break;
            case 5:
                Tool.setStringToText(tvJianliWorkXue, "硕士");

                break;
            case 6:
                Tool.setStringToText(tvJianliWorkXue, "博士");

                break;
        }


        toolBarTvTitle.setText("教育经历");
        toolBarTvMore.setText("保存");
        if (popup == null) {
            popup = new PopupForChangeInfo(getActivity(), new PopupForChangeInfo.ChangeCallBack() {
                @Override
                public void call(String comment) {
                    if (comment == null || comment.equals("")) {
                        LogUtils.startToash(getContext(), "填写的信息为空");
                    } else {
                        switch (type) {
                            case 0:
                                tvJianliEducationName.setText(comment);
                                jianLiEduModle.setSchool(comment);
                                break;
                            case 1:
                                tvJianliEducationZhuanYe.setText(comment);
                                jianLiEduModle.setProfession(comment);

                                break;

                        }
                    }
                }
            });
        }


    }

    private void initView(View rootView) {
        tvJianliEducationName = (TextView) rootView.findViewById(R.id.tv_jianli_education_name);
        tvJianliEducationName.setOnClickListener(Fragment_JianLi_AddEducation.this);
        tvJianliEducationZhuanYe = (TextView) rootView.findViewById(R.id.tv_jianli_education_ZhuanYe);
        tvJianliEducationZhuanYe.setOnClickListener(Fragment_JianLi_AddEducation.this);
        tvJianliEducationTimeOut = (TextView) rootView.findViewById(R.id.tv_jianli_education_time_out);
        tvJianliEducationTimeOut.setOnClickListener(Fragment_JianLi_AddEducation.this);
        tvJianliWorkXue = (TextView) rootView.findViewById(R.id.tv_jianli_work_xue);
        tvJianliWorkXue.setOnClickListener(Fragment_JianLi_AddEducation.this);
        toolbarImageBack = (ImageView) rootView.findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(Fragment_JianLi_AddEducation.this);
        toolBarTvTitle = (TextView) rootView.findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) rootView.findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_More);
        toolbarLayoutMore.setOnClickListener(Fragment_JianLi_AddEducation.this);
        toolbarContent = (LinearLayout) rootView.findViewById(R.id.toolbar_Content);
        btnDeleteEdu = (TextView) rootView.findViewById(R.id.btn_delete_edu);
        btnDeleteEdu.setOnClickListener(Fragment_JianLi_AddEducation.this);

    }


    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.tv_jianli_education_name) {
            clickName();
        } else if (view.getId() == R.id.tv_jianli_education_ZhuanYe) {
            clickZhuanYe();


        } else if (view.getId() == R.id.tv_jianli_education_time_out) {
            clickTime();
        } else if (view.getId() == R.id.tv_jianli_work_xue) {
            clickXueLi();
        } else if (view.getId() == R.id.toolbar_Layout_back) {
            getActivity().finish();

        } else if (view.getId() == R.id.toolbar_Layout_More) {
            clickBaoCun();

        } else if (view.getId() == R.id.btn_delete_edu) {
            DeleteWork();

        }
    }

    private void clickBaoCun() {
        jianLiEduModle.setMember_id(SharedPreferenceUtils.getMemberId(getContext()));
        if (jianLiEduModle.getEdu_id() == 0) {
            addEdu();
        }


    }

    private void addEdu() {
        String s = new Gson().toJson(jianLiEduModle);
        UserServiceImpl.addJianLiEdu(s, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String string = null;
                try {
                    ResponseBody body = response.body();
                    if (body == null) {
                        ResponseBody responseBody = response.errorBody();
                        if (responseBody != null) {
                            String string1 = responseBody.string();
                            LogUtils.e("错误信息" + string1);
                        }
                    } else {
                        string = body.string();
                        LogUtils.e(string);
                        getActivity().finish();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("错误信息" + t.getMessage());

            }
        });


    }

    private void clickName() {
        type = 0;
        showEdit(tvJianliEducationName, 0, "请输入姓名");
    }

    private void clickZhuanYe() {
        type = 1;
        showEdit(tvJianliEducationName, 1, "请输入专业");
    }

    private void clickTime() {
        if (timeSelectUtils == null) {
            timeSelectUtils = new TimeSelectUtils();
        }
        timeSelectUtils.selectTime(getContext(), new TimeSelectUtils.TimeCallBack() {
            @Override
            public void seclectCall(DatePicker view, int year, int month, int dayOfMonth, String time) {
                tvJianliEducationTimeOut.setText(time + "");
                jianLiEduModle.setGraduate_dt(time);
            }
        });
    }

    private void clickXueLi() {
        /// 最高学历 0未知(默认值) 1 中专及以下 2高中 3大专 4本科 5硕士 6博士
        if (popupXueli == null) {
            String[] xueli = {"中专及以下", "高中", "大专", "本科", "硕士", "博士"};
            popupXueli = new PopupForAddress(getActivity(), xueli, new PopupForAddress.ShareModleCallBack() {
                @Override
                public void callSure(int position, String comment) {
                    popupXueli.dismiss();
                    tvJianliWorkXue.setText(comment);
                    jianLiEduModle.setEdu(position + 1);


                }
            });
        }

        popupXueli.show(tvJianliWorkXue);
    }


    public void showEdit(View view, int type, String title) {
        this.type = type;
        popup.setTitle(title);
        popup.openKeyboard();
        Tool.setAlpha(getActivity(), 0.6f);
        popup.showAtLocation(view,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }


    public void DeleteWork() {
        if (jianLiEduModle.getEdu_id() == 0) {
            getActivity().finish();
        } else {
            UserServiceImpl.deleteJianLiEdu(jianLiEduModle.getEdu_id() + "", new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    String string = null;
                    try {
                        ResponseBody body = response.body();
                        if (body == null) {
                            ResponseBody responseBody = response.errorBody();
                            if (responseBody != null) {
                                String string1 = responseBody.string();
                                LogUtils.e("错误信息" + string1);
                            }
                        } else {
                            string = body.string();
                            LogUtils.e(string);
                            getActivity().finish();
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    LogUtils.e("错误信息" + t.getMessage());

                }
            });
        }
    }

}
